import {xyseed,boom} from './tool.js'
import { set_score,boom_list,award_list ,boss_bullet_list} from './canvas.js';
import { power_up,wingman_buff } from './award.js';
export const enemy_list=[]

var max_width = document.body.offsetWidth;
var max_height = document.documentElement.clientHeight;
  
function  y_enemy1(user_plan1){
    this.life=5
    this.speed = 4;
    this.top = 0;
    this.left = 0;
    this.width = 136;
    this.height = 92;
    this.create = (x,y) => {
      this.dom = document.getElementById("y_enemy1");
      this.top = Math.ceil(Math.random() * -1);
      this.left=Math.ceil(Math.random() * max_width-this.width);
      this.time=setInterval(()=>{
        this.top+=this.speed
      },20)
      this.time=setTimeout(()=>{
        let bullet = new enemy_bullet1()
        let data=xyseed(user_plan1,{left,top,width,height})
        bullet.create(left+width/2,top+height,data[0],data[1])
        boss_bullet_list.push(bullet)
      },500)
    };
    die=()=>{
      clearInterval(this.time)
      return 3
    }
  }
  function  y_enemy2(user_plan1){
    this.life=8
    this.speed = 4;
    this.top = 0;
    this.left = 0;
    this.width = 127;
    this.height = 108;
    this.create = (x,y) => {
      this.dom = document.getElementById("y_enemy2");
      this.top = Math.ceil(Math.random() * -200);
      this.left=Math.ceil(Math.random() * max_width-this.width);
      this.time=setInterval(()=>{
        this.top+=this.speed
        
      },20)
    };
    this.die=()=>{
      clearInterval(this.time)
      create_award(this.top,this.left,user_plan1)
      set_score(5) 
    }
  }
  function  enemy1(user_plane){
    this.life=8
    this.xspeed=0
    this.yspeed = 4;
    this.dom = document.getElementById("enemy1");
    this.width =149
    this.height = 86;
    this.top = Math.ceil(Math.random() * -200);
    this.left=Math.ceil(Math.random() * max_width-this.width);
    this.active=()=>{
      this.time= setTimeout(() => {
        console.log("发送子弹")
        const bullet=new enemy_bullet1(user_plane);
        const xy=xyseed(user_plane,this)
        bullet.create(this.left+this.width/2,this.top+this.height,xy[0],xy[1])
        boss_bullet_list.push(bullet)
      }, 2000);
    }
    this.move = () => {
        this.top+=this.yspeed
        this.left+=this.xspeed
    };
    this.die=()=>{
      clearTimeout(this.time)
      enemy_boom(this)
      create_award(this.top,this.left,user_plane)
      set_score(5) 
    }
  }
  function  enemy2(user_plane){
    this.life=8
    this.xspeed=0
    this.yspeed = 4;
    this.dom = document.getElementById("enemy2");
    this.width =100;
    this.height =85;
    this.top = Math.ceil(Math.random() * -200);
    this.left=Math.ceil(Math.random() * max_width-this.width);
    
    
    this.move = () => {
        this.top+=this.yspeed
        this.left+=this.xspeed
    };
    this.die=()=>{
      enemy_boom(this)
      create_award(this.top,this.left,user_plane)
      set_score(5) 
    }
  }
  function  enemy3(user_plane){
    this.life=8
    this.xspeed=0
    this.yspeed = 4;
    this.dom = document.getElementById("enemy3");
    this.width =128;
    this.height = 94;
    this.top = Math.ceil(Math.random() * -200);
    this.left=Math.ceil(Math.random() * max_width-this.width);
    
    
    this.move = () => {
        this.top+=this.yspeed
        this.left+=this.xspeed
    };
    this.die=()=>{
      enemy_boom(this)
      create_award(this.top,this.left,user_plane)
      set_score(5) 
    }
  }

  class enemy_bullet1 {
  constructor(user_plane) {
    this.width = 10;
    this.height = 10;
    this.top = null;
    this.left = null;
    this.speed = 10;
    this.deg = 0;
    this.atk = 2;
    this.xspeed = null;
    this.yspeed = null;
    this.dom = document.getElementById("enemy_bullet1");
  }
  create = (x, y, xs, ys) => {
    this.left = x;
    this.top = y;
    this.xspeed = xs * 5;
    this.yspeed = ys * 5;
    this.move();
  };
  move = () => {
    this.time = setInterval(() => {
      this.top += this.yspeed;
      this.left += this.xspeed;
    }, 20);
  };
}
  function create_award(top,left,user_plane){
    
    let random=Math.ceil(Math.random()*10)
    switch(random){
      case 1:
        var award = new power_up(top,left)
        award_list.push(award)
        break
      case 2:
        if(user_plane.wingman){
          break
        }
        var award = new wingman_buff(top,left)
        award_list.push(award)
        break
    }
  }
  function enemy_boom(item){
    let boom1=new boom(item)
    boom_list.push(boom1)
  }
export default {enemy1,enemy2,enemy3}